
import { Navigate, Route, Routes } from "react-router-dom";
import { observer } from "mobx-react";
import { ConfigProvider } from '@/components/base';
import { Controller } from '@/controller';
import zhCN from 'antd/locale/zh_CN';
import React from "react";
import { decorator } from "@/tools/fns";
import * as Views from "@/views";
import 'dayjs/locale/zh-cn';

export const App = () => {

  useCompany();

  return (
    <Main />
  )
}

const Main = decorator(observer)(() => {
  return (
    <ConfigProvider locale={zhCN} theme={{}} >
      {
        Controller.instance.token ?
          (
            <Routes>
              <Route path='/home' element={<Views.Home />} />
              <Route path='/m/*' element={<Views.Container />} />
              <Route path='*' element={<Navigate to="/home" />} />
            </Routes>
          ) : (
            <Routes>
              <Route path='/open-in-browser' element={<Views.OpenInBrowser />} />  {/* 在浏览器打开 */}
              <Route path='/login-browser' element={<Views.LoginBrowser />} />     {/* 登录页面(浏览器) */}
              <Route path='*' element={<Views.Login />} />                         {/* 登录页面 */}
            </Routes>
          )
      }
    </ConfigProvider>
  )
})

/** 从地址栏获取公司标识并存储 */
function useCompany() {
  React.useEffect(() => {
    const company = new URLSearchParams(window.location.search).get('c');
    if (company) {
      Controller.instance.company = company;
      window.sessionStorage.setItem('$$/company-id', company);
    }
  }, [])
}